<?php
defined('IN_IA') or exit('Access Denied');
global $_W, $_GPC;
$_W['page']['title'] = '顾客管理-' . $_W['we7_wmall']['config']['title'];
mload()->model('smember');
$do = 'smember';
$op = trim($_GPC['op']) ? trim($_GPC['op']) : 'sync';
if ($op == 'sync') {
	if ($_W['isajax']) {
		$uid = intval($_GPC['__input']['uid']);
		$update = array();
		$update['success_num'] = intval(pdo_fetchcolumn('select count(*) from ' . tablename('tiny_wmall_order') . ' where uniacid = :uniacid and uid = :uid and is_pay = 1 and status = 5', array(':uniacid' => $_W['uniacid'], ':uid' => $uid)));
		$update['success_price'] = floatval(pdo_fetchcolumn('select sum(final_fee) from ' . tablename('tiny_wmall_order') . ' where uniacid = :uniacid and uid = :uid and is_pay = 1 and status = 5', array(':uniacid' => $_W['uniacid'], ':uid' => $uid)));
		$update['cancel_num'] = intval(pdo_fetchcolumn('select count(*) from ' . tablename('tiny_wmall_order') . ' where uniacid = :uniacid and uid = :uid and status = 6', array(':uniacid' => $_W['uniacid'], ':uid' => $uid)));
		$update['cancel_price'] = floatval(pdo_fetchcolumn('select sum(final_fee) from ' . tablename('tiny_wmall_order') . ' where uniacid = :uniacid and uid = :uid and status = 6', array(':uniacid' => $_W['uniacid'], ':uid' => $uid)));
		pdo_update('tiny_wmall_members', $update, array('uniacid' => $_W['uniacid'], 'uid' => $uid));
		message(error(0, ''), '', 'ajax');
	}
	$uids = pdo_getall('tiny_wmall_members', array('uniacid' => $_W['uniacid']), array('uid'), 'uid');
	$uids = array_keys($uids);
}
if ($op == 'list') {
	$condition = ' where uniacid = :uniacid';
	$params = array(':uniacid' => $_W['uniacid']);
	$keyword = trim($_GPC['keyword']);
	$member_level = trim($_GPC['member_level']);
	if (!empty($keyword)) {
		$condition.= ' and (realname like :keyword or mobile like :keyword)';
		$params[':keyword'] = "%{$keyword}%";
	}
	if (!empty($member_level)) {
		$condition.= ' and (member_level = '.$member_level.')';
	}
	$setmeal_status = isset($_GPC['setmeal_status']) ? intval($_GPC['setmeal_status']) : -1;
	if ($setmeal_status > 0) {
		$setmeal_id = isset($_GPC['setmeal_id']) ? intval($_GPC['setmeal_id']) : -1;
		if ($setmeal_id > 0) {
			$condition.= ' and setmeal_id = :setmeal_id';
			$params[':setmeal_id'] = $setmeal_id;
		} else {
			$condition.= ' and setmeal_id > 0';
		}
		$endtime = isset($_GPC['endtime']) ? intval($_GPC['endtime']) : -1;
		if ($endtime >= 0) {
			$condition.= ' and setmeal_endtime <= :setmeal_endtime';
			$params[':setmeal_endtime'] = strtotime("{$endtime}days", strtotime(date('Y-m-d')));
		}
	}
	$sort = trim($_GPC['sort']);
	$sort_val = intval($_GPC['sort_val']);
	if (!empty($sort)) {
		if ($sort_val == 1) {
			$condition.= " ORDER BY {$sort} DESC";
		} else {
			$condition.= " ORDER BY {$sort} ASC";
		}
	}

	$pindex = max(1, intval($_GPC['page']));
	$psize = 40;
	$total = pdo_fetchcolumn('select count(*) from ' . tablename('tiny_wmall_members') . $condition, $params);
	$data = pdo_fetchall('select * from ' . tablename('tiny_wmall_members') . $condition . ' LIMIT ' . ($pindex - 1) * $psize . ',' . $psize, $params);
	foreach ($data as $key => &$value)
	{
		$temp = pdo_fetch("SELECT levelname,id FROM ". tablename("tiny_wmall_member_level"). " WHERE uniacid={$_W['uniacid']} AND id={$value['member_level']}");
		$value['levelname'] = $temp['levelname'];
		$value['level_id'] = $temp['id'];
	}
	unset($value);

	$pager = pagination($total, $pindex, $psize);
	$stat = smember_amount_stat($sid, $id);
	$cards = pdo_fetchall('select * from ' . tablename('tiny_wmall_delivery_cards') . ' where uniacid = :uniacid order by displayorder desc, id asc', array(':uniacid' => $_W['uniacid']), 'id');
}
if ($op == 'stat') {
	$start = $_GPC['start'] ? strtotime($_GPC['start']) : strtotime(date('Y-m'));
	$end = $_GPC['end'] ? strtotime($_GPC['end']) + 86399 : (strtotime(date('Y-m-d')) + 86399);
	$day_num = ($end - $start) / 86400;
	if ($_W['isajax'] && $_W['ispost']) {
		$days = array();
		$datasets = array('flow1' => array(),);
		for ($i = 0;$i < $day_num;$i++) {
			$key = date('m-d', $start + 86400 * $i);
			$days[$key] = 0;
			$datasets['flow1'][$key] = 0;
		}
		$data = pdo_fetchall("SELECT * FROM " . tablename('tiny_wmall_members') . 'WHERE uniacid = :uniacid AND first_order_time >= :starttime and first_order_time <= :endtime', array(':uniacid' => $_W['uniacid'], ':starttime' => $start, 'endtime' => $end));
		foreach ($data as $da) {
			$key = date('m-d', $da['addtime']);
			if (in_array($key, array_keys($days))) {
				$datasets['flow1'][$key]++;
			}
		}
		$shuju['label'] = array_keys($days);
		$shuju['datasets'] = $datasets;
		exit(json_encode($shuju));
	}
	$stat = smember_amount_stat();
}
if ($op == 'card_order') {
	$condition = ' where uniacid = :uniacid';
	$params = array(':uniacid' => $_W['uniacid']);
	$keyword = trim($_GPC['keyword']);
	if (!empty($keyword)) {
		$condition.= ' and uid in (select uid from ' . tablename('tiny_wmall_members') . ' where uniacid = :uniacid and (realname like :keyword or mobile like :keyword))';
		$params[':keyword'] = "%{$keyword}%";
	}
	$uid = intval($_GPC['uid']);
	if ($uid > 0) {
		$condition.= ' and uid = :uid';
		$params[':uid'] = $uid;
	}
	$setmeal_id = isset($_GPC['setmeal_id']) ? intval($_GPC['setmeal_id']) : -1;
	if ($setmeal_id > 0) {
		$condition.= ' and card_id = :setmeal_id';
		$params[':setmeal_id'] = $setmeal_id;
	}
	$paytime = isset($_GPC['paytime']) ? intval($_GPC['paytime']) : -1;
	if ($paytime > 0) {
		$condition.= ' and paytime >= :paytime';
		$params[':paytime'] = strtotime("-{$paytime}days", strtotime(date('Y-m-d')));
	}
	$pindex = max(1, intval($_GPC['page']));
	$psize = 40;
	$total = pdo_fetchcolumn('select count(*) from ' . tablename('tiny_wmall_delivery_cards_order') . $condition, $params);
	$orders = pdo_fetchall('select * from ' . tablename('tiny_wmall_delivery_cards_order') . $condition . ' ORDER BY id desc LIMIT ' . ($pindex - 1) * $psize . ',' . $psize, $params);
	if (!empty($orders)) {
		$uids = array();
		foreach ($orders as $order) {
			$uids[] = $order['uid'];
		}
		$uids = implode(',', array_unique($uids));
		$users = pdo_fetchall('select id, uid, realname, avatar from ' . tablename('tiny_wmall_members') . " where uniacid = :uniacid and uid in ({$uids})", array(':uniacid' => $_W['uniacid']), 'uid');
	}
	$pager = pagination($total, $pindex, $psize);
	$cards = pdo_fetchall('select * from ' . tablename('tiny_wmall_delivery_cards') . ' where uniacid = :uniacid order by displayorder desc, id asc', array(':uniacid' => $_W['uniacid']), 'id');
	$pay_types = order_pay_types();
}
if($op == "mem_level")
{
	$lists = pdo_fetchall("SELECT * FROM ". tablename("tiny_wmall_member_level"). " WHERE uniacid={$_W['uniacid']}");
}
if($op == "add_level")
{
	if(checksubmit('submit'))
	{
		$data['id'] = $_POST['id'];
		if(empty($data['id']))
		{
			//插入
			$data['levelname'] = $_GPC['levelname'];
			$data['discount'] = $_GPC['discount'];
			$data['uniacid'] = $_W['uniacid'];
			$data['ordercount'] = $_GPC['ordercount'];
			$insertId = pdo_insert("tiny_wmall_member_level",$data);
			if($insertId)
			{
				$url = $this->createWebUrl('ptfmember', array('op' => 'mem_level'));
				message('添加成功!',$url);
			}
		}
		else
		{
			//更新
			$data['levelname'] = $_GPC['levelname'];
			$data['discount'] = $_GPC['discount'];
			$data['uniacid'] = $_W['uniacid'];
			$data['ordercount'] = $_GPC['ordercount'];
			$res = pdo_update("tiny_wmall_member_level",$data,array('id'=>$data['id']));
			if($res)
			{
				$url = $this->createWebUrl('ptfmember', array('op' => 'mem_level'));
				message('更新成功!',$url);
			}
		}
	}

	$id = $_GPC['id'];
	if(!empty($id))
	{
		$info = pdo_fetch("SELECT * FROM ". tablename("tiny_wmall_member_level"). " WHERE uniacid={$_W['uniacid']} and id={$id}");
	}
}
if($op == 'del_level')
{
	$id = $_GPC['id'];
	if(!empty($id))
	{
		$res = pdo_delete("tiny_wmall_member_level",array('id'=>$id));
		if($res)
		{
			$url = $this->createWebUrl('ptfmember', array('op' => 'mem_level'));
			message('删除成功!',$url);
		}
	}
}

if($op == "ajax_get_member_level")
{
	$id = $_GPC['id'];
	if(!isset($id))
	{
		echo json_encode(array('status'=>0));
		exit();
	}
	//取得会员等级列表
	$levels = pdo_fetchall("SELECT * FROM ". tablename("tiny_wmall_member_level"). " WHERE uniacid={$_W['uniacid']}");
	//取得当前会员等级
	$self_level = pdo_fetchcolumn("SELECT member_level FROM ". tablename("tiny_wmall_members"). " WHERE uniacid={$_W['uniacid']} and id={$id}");
	if($levels)
	{
		echo json_encode(array('status'=>1,'list'=>$levels,"level"=>$self_level));
		exit();
	}
}
if($op == "ajax_update_member_level")
{
	$level = $_GPC['level'];
	$id = $_GPC['id'];
	if(!isset($level) || !isset($id))
	{
		echo json_encode(array('status'=>0));
		exit();
	}

	//取得会员等级列表
	$self_level = pdo_fetchcolumn("SELECT member_level FROM ". tablename("tiny_wmall_members"). " WHERE uniacid={$_W['uniacid']} and id={$id}");
	
	$res = pdo_update("tiny_wmall_members",array("member_level"=>$level),array('id'=>$id));
	$levelname = pdo_fetchcolumn("SELECT levelname FROM ". tablename("tiny_wmall_member_level"). " WHERE uniacid={$_W['uniacid']} and id={$level}");
	if(!$levelname)
	{
		$levelname = "普通会员";
	}

	if($level == $self_level)
	{
		echo json_encode(array('status'=>1,'levelname'=>$levelname));
		exit();
	}

	if($res)
	{
		echo json_encode(array('status'=>1,'levelname'=>$levelname));
		exit();
	}
}

include $this->template('plateform/member');
?>